## This code reproduces Figure 2, Panel B, and associated ANAVA ##

library(foreign)
library(optiscale)
library(lattice)

ValRanks <- read.table(file.choose(), header = TRUE)

PartyIdeolDat <- read.spss(file.choose(), to.data.frame = TRUE)

vars <- c("pid7", "ideo5", "CaseID")

SmallDat <- PartyIdeolDat[vars]

ValRanks$CaseID <- ValRanks$caseid

newdata <- merge(ValRanks, SmallDat, by = "CaseID")

## Coding sorted/unsorted ##

table(newdata$ideo5)
newdata$Lib[newdata$ideo5 == "Very liberal"] <- 1
newdata$Lib[newdata$ideo5 == "Liberal"] <- 1
newdata$Lib[newdata$ideo5 == "Moderate"] <- 0
newdata$Lib[newdata$ideo5 == "Conservative"] <- 0
newdata$Lib[newdata$ideo5 == "Very Conservative"] <- 0
newdata$Lib[newdata$ideo5 == "Not sure"] <- 0
table(newdata$Lib)

newdata$Con[newdata$ideo5 == "Very liberal"] <- 0
newdata$Con[newdata$ideo5 == "Liberal"] <- 0
newdata$Con[newdata$ideo5 == "Moderate"] <- 0
newdata$Con[newdata$ideo5 == "Conservative"] <- 1
newdata$Con[newdata$ideo5 == "Very Conservative"] <- 1
newdata$Con[newdata$ideo5 == "Not sure"] <- 0
table(newdata$Con)

newdata$Mod[newdata$ideo5 == "Very liberal"] <- 0
newdata$Mod[newdata$ideo5 == "Liberal"] <- 0
newdata$Mod[newdata$ideo5 == "Moderate"] <- 1
newdata$Mod[newdata$ideo5 == "Conservative"] <- 0
newdata$Mod[newdata$ideo5 == "Very Conservative"] <- 0
newdata$Mod[newdata$ideo5 == "Not sure"] <- 0
table(newdata$Mod)

table(newdata$pid7)
newdata$Dem[newdata$pid7 == "Strong Democrat"] <- 1
newdata$Dem[newdata$pid7 == "Weak Democrat"] <- 1
newdata$Dem[newdata$pid7 == "Lean Democrat"] <- 1
newdata$Dem[newdata$pid7 == "Independent"] <- 0
newdata$Dem[newdata$pid7 == "Lean Republican"] <- 0
newdata$Dem[newdata$pid7 == "Weak Republican"] <- 0
newdata$Dem[newdata$pid7 == "Strong Republican"] <- 0
newdata$Dem[newdata$pid7 == "Not sure"] <- 0
newdata$Dem[newdata$pid7 == "Don't know"] <- 0
table(newdata$Dem)

newdata$Rep[newdata$pid7 == "Strong Democrat"] <- 0
newdata$Rep[newdata$pid7 == "Weak Democrat"] <- 0
newdata$Rep[newdata$pid7 == "Lean Democrat"] <- 0
newdata$Rep[newdata$pid7 == "Independent"] <- 0
newdata$Rep[newdata$pid7 == "Lean Republican"] <- 1
newdata$Rep[newdata$pid7 == "Weak Republican"] <- 1
newdata$Rep[newdata$pid7 == "Strong Republican"] <- 1
newdata$Rep[newdata$pid7 == "Not sure"] <- 0
newdata$Rep[newdata$pid7 == "Don't know"] <- 0

newdata$Ind[newdata$pid7 == "Strong Democrat"] <- 0
newdata$Ind[newdata$pid7 == "Weak Democrat"] <- 0
newdata$Ind[newdata$pid7 == "Lean Democrat"] <- 0
newdata$Ind[newdata$pid7 == "Independent"] <- 1
newdata$Ind[newdata$pid7 == "Lean Republican"] <- 0
newdata$Ind[newdata$pid7 == "Weak Republican"] <- 0
newdata$Ind[newdata$pid7 == "Strong Republican"] <- 0
newdata$Ind[newdata$pid7 == "Not sure"] <- 0
newdata$Ind[newdata$pid7 == "Don't know"] <- 0

newdata$StrongSortedDem <- ifelse(newdata$pid7 == "Strong Democrat" &
                                    newdata$ideo5 == "Very liberal", 1, 0)
table(newdata$StrongSortedDem)
newdata$WeakSortedDem <- ifelse(newdata$Dem == 1 & newdata$Lib == 1 & 
                                  newdata$StrongSortedDem == 0, 1, 0)
table(newdata$WeakSortedDem)
newdata$UnsortedDem <- ifelse(newdata$Dem == 1 & newdata$Lib == 0, 1, 0)
table(newdata$UnsortedDem)
newdata$StrongSortedRep <- ifelse(newdata$pid7 == "Strong Republican" &
                                    newdata$ideo5 == "Very Conservative", 1, 0)
table(newdata$StrongSortedRep)
newdata$WeakSortedRep <- ifelse(newdata$Rep == 1 & newdata$Con == 1 &
                                  newdata$StrongSortedRep == 0, 1, 0)
table(newdata$WeakSortedRep)
newdata$UnsortedRep <- ifelse(newdata$Rep == 1 & newdata$Con == 0, 1, 0)
table(newdata$UnsortedRep)

newdata$SortedParty <- NA
newdata$SortedParty[newdata$StrongSortedDem == 1] <- "SSD"
newdata$SortedParty[newdata$WeakSortedDem == 1] <- "WSD"
newdata$SortedParty[newdata$UnsortedDem == 1] <- "UD"
newdata$SortedParty[newdata$Ind == 1] <- "I"
newdata$SortedParty[newdata$StrongSortedRep == 1] <- "SSR"
newdata$SortedParty[newdata$WeakSortedRep == 1] <- "WSR"
newdata$SortedParty[newdata$UnsortedRep == 1] <- "UR"
table(newdata$SortedParty)

Vars <- c("SortedParty", "rankfr", "rankeq", "rankes", "rankmo", "rankin", "rankso", "rankpa")

FullData <- newdata[Vars]
FullData <- na.omit(FullData)

FullData$AllSame  <- ifelse(FullData$rankfr == FullData$rankeq &
                              FullData$rankfr == FullData$rankes &
                              FullData$rankfr == FullData$rankmo &
                              FullData$rankfr == FullData$rankin &
                              FullData$rankfr == FullData$rankso &
                              FullData$rankfr == FullData$rankpa, 1, 0)
table(FullData$AllSame)

FullData <- FullData[which(FullData$AllSame == 0), ]

FullData$AllSame <- NULL

ValsOnly <- c("rankfr", "rankeq", "rankes", "rankmo", "rankin", "rankso", "rankpa")
ValsData <- FullData[ValsOnly]


ValsData[1:10, ]

###

###
###   Standardize data within rows
###

vals.std <- t(apply(ValsData, 1, scale))

vals.std[1:10,]

###
###   Initialize the matrix of optimally-scaled
###   values, the previous fit, the iteration
###   number, and the improvement in fit over
###   the previous iteration
###

vals.os <- vals.std

prev.fit2 <- 0

niter <- 0

improve = 1

###
###   Start iterations
###

while (improve > .01 & niter <= 25) {
  niter <- niter + 1
  
  ###
  ###   Perform SVD on OS version of rank-orders
  ###
  
  decomp <- svd(vals.os)
  
  ###
  ###   Calculate 2-dimensional goodness-of-fit
  ###   and improvement in fit over previous iteration
  ###
  
  d.sqd <- decomp$d^2
  
  fit2 <- sum(d.sqd[1:2]) / sum(d.sqd)
  
  fitvector <- cumsum(d.sqd) / rep(sum(d.sqd), length(d.sqd))
  
  improve <- fit2 - prev.fit2
  
  ###
  ###   Create iteration history
  ###
  
  if (niter == 1) {
    history <- c(niter, fit2, improve)
  }
  if (niter > 1)  {
    history <- rbind(history, c(niter, fit2, improve))
  }
  
  ###
  ###   Obtain terminal points of vectors for row objects
  ###   (respondents in this case), calculate predicted
  ###   ranks, norm the row object vectors to unit length
  ###
  
  subj.c1 <- decomp$u[, 1:2] %*% diag(decomp$d[1:2])
  
  pred.vals <- subj.c1 %*% t(decomp$v[,1:2])
  
  sumsqd <- apply(subj.c1^2, 1, sum)
  
  root.sumsqd <- (matrix(sumsqd, nrow = length(sumsqd), ncol = 1)) ^ .5
  
  subj.coord <- subj.c1 / (root.sumsqd %*% matrix(1, nrow = 1, ncol = 2))
  
  ###
  ###   Obtain new optimally scaled data values,
  ###   using predicted ranks from fitted model
  ###
  
  for (i in 1:nrow(vals.os)) {
    opped <- opscale(x.qual = vals.std[i,],
                     x.quant = pred.vals[i,],
                     level = 2,
                     process = 1,
                     rescale = T)
    vals.os[i,] <- opped$os
  }
  
  ###
  ###   Set current fit to previous fit for next iteration
  ###
  
  
  prev.fit2 <- fit2
  
}

###
###   Print iteration history
###

history

###
###   The "fitvector" object shows the R-squared
###   for the solution in each dimensionality
###

fitvector

###
###   Plot value points
###

val.coords <- as.data.frame(decomp$v[, 1:2])

rownames(val.coords) <- colnames(ValsData)

val.coords

xyplot(V2 ~ V1, val.coords,
       aspect = 1,
       xlim = c(-.99, .99),
       ylim = c(-.99, .99),
       panel = function (x, y) {
         panel.xyplot(x, y, col = "black")
         panel.text(x, y, labels = rownames(val.coords),
                    pos = 1, cex = .75)
       }
)

###
###   Plot terminal points of row object vectors
###   in same space as value points
###

subj.coord <- as.data.frame(subj.coord)

set.seed(123)
xyplot(V2 ~ V1, val.coords,
       aspect = 1,
       xlim = c(-1.2, 1.2),
       ylim = c(-1.2, 1.2),
       panel = function (x, y) {
         panel.xyplot(x, y, col = "black", pch = 16)
         panel.xyplot(jitter(subj.coord$V1, amount = .05), 
                      jitter(subj.coord$V2, amount = .05), col = "black")
         panel.text(x, y, labels = rownames(val.coords),
                    pos = 1, cex = .75)
       }
)

###
###   Calculate overall mean direction and
###   mean resultant length
###

meand1 <- mean(subj.coord$V1)

meand2 <- mean(subj.coord$V2)

meand1 

meand2

mean.length <- (meand1^2 + meand2^2)^.5

mean.length

###
###   Plot mean vector along with 
###   individual vectors and value points
###

set.seed(123)
xyplot(V2 ~ V1, val.coords,
       aspect = 1,
       xlim = c(-1.2, 1.2),
       ylim = c(-1.2, 1.2),
       panel = function (x, y) {
         panel.xyplot(x, y, col = "black", pch = 16)
         panel.xyplot(jitter(subj.coord$V1, amount = .05), 
                      jitter(subj.coord$V2, amount = .05), col = "black")
         panel.text(x, y, labels = rownames(val.coords),
                    pos = c(4,1,3,3,2), cex = .75)
         panel.arrows(0, 0, meand1, meand2, angle = 15, length = .15)
       }
)

### Plotting Vectors ###

### merging some data ###
# val.coords is vector model results, plotting points for values in 2d space
# vals is TESS data with NaNs omitted (see VectorModel.r); n = 622
# subj.coord is subject coordinates in 2d space (see VectorModel.r)

demos <- c("SortedParty")

vals2 <- FullData[demos]

vals3 <- data.frame(cbind(vals2, subj.coord))

### Graph for Party ###
# Creating 3 Category Party Var #

#vals3$party3 <- "Independent"
#vals3$party3[vals3$party >= 1 & vals3$party <= 2] <- "Republican"
#vals3$party3[vals3$party >= 6 & vals3$party <= 7] <- "Democrat"

# Now calculate mean vectors for respondents in the three categories. Also
# calculate mean resultant lengths for the category vectors.

mean.pty.d1 <- tapply(vals3$V1, vals3$SortedParty, mean, na.rm = T)

mean.pty.d2 <- tapply(vals3$V2, vals3$SortedParty, mean, na.rm = T)

pty.mean.lengths <- (mean.pty.d1^2 + mean.pty.d2^2)^.5

# All Respondents
g2 <- xyplot(V1 ~ V2, data = vals3,
             aspect = 1, 
             #main = "By Party and Sorting",
             panel = function (x, y) {
               panel.xyplot(val.coords$V1, val.coords$V2, pch = 16, 
                            cex = .5, col = "black")
               panel.text(val.coords$V1, val.coords$V2, 
                          labels = c("Free", "Eq", "ES",  
                                     "Mo", "Ind", "SO", "Pat"),
                          pos = c(4, 4, 1, 3, 4, 4, 3), cex = .75)
               panel.segments(rep(0, 7), rep(0, 7), 
                              mean.pty.d1[c(1,2,3,4,5,6,7)], mean.pty.d2[c(1,2,3,4,5,6,7)], lwd = 1.5)
               panel.text(mean.pty.d1[c(1,2,3,4,5,6,7)], mean.pty.d2[c(1,2,3,4,5,6,7)], 
                          labels = c("Ind", "SSD", "SSR", 
                                     "UD", "UR", "WSD", "WSR"), 
                          pos = c(3,3,2,4,1,4,1), cex = 1)
             },
             xlab = "",
             ylab = "",
             scales = list(draw = FALSE)
)
g2

# All 7 vectors #
meand1 <- mean(vals3$V1)

meand2 <- mean(vals3$V2)

mean.res.length <- (meand1^2 + meand2^2)^.5

res.length <- length(vals3$V1) * mean.res.length

ideol.mean.lengths <- c(.290, .811, .677, .630, .089, .760, .494)

incparty.between <- sum(ideol.mean.lengths * as.vector(table(vals3$SortedParty))) - res.length

incparty.within <- length(vals3$V1) - sum(ideol.mean.lengths * as.vector(table(vals3$SortedParty)))

dfnum <- 6

dfdenom <- length(vals3$V1) - 7

incparty.ms.between <- incparty.between / dfnum

incparty.ms.within <- incparty.within / dfdenom

F.incparty <- incparty.ms.between / incparty.ms.within

obs.prob.incparty <- 1 - pf(F.incparty, dfnum, dfdenom)

# F on 6 and 747 df = 53.81, p = 0

### Just Repubs

meand1 <- mean(vals3$V1[vals3$SortedParty == "SSR" | vals3$SortedParty == "UR" |
                          vals3$SortedParty == "WSR"])

meand2 <- mean(vals3$V2[vals3$SortedParty == "SSR" | vals3$SortedParty == "UR" |
                          vals3$SortedParty == "WSR"])

mean.res.length <- (meand1^2 + meand2^2)^.5

res.length <- length(vals3$V1[vals3$SortedParty == "SSR" | vals3$SortedParty == "UR" |
                                vals3$SortedParty == "WSR"]) * mean.res.length

ideol.mean.lengths <- c(.677, .089, .494)

incparty.between <- sum(ideol.mean.lengths * 
                          as.vector(table(vals3$SortedParty[vals3$SortedParty == "SSR" | 
                                                              vals3$SortedParty == "UR" |
                                                              vals3$SortedParty == "WSR"]))) - 
  res.length

incparty.within <- length(vals3$V1[vals3$SortedParty == "SSR" | 
                                     vals3$SortedParty == "UR" |
                                     vals3$SortedParty == "WSR"]) - 
  sum(ideol.mean.lengths * 
        as.vector(table(vals3$SortedParty[vals3$SortedParty == "SSR" | 
                                            vals3$SortedParty == "UR" | 
                                            vals3$SortedParty == "WSR"])))

dfnum <- 2

dfdenom <- length(vals3$V1[vals3$SortedParty == "SSR" | vals3$SortedParty == "UR" |
                             vals3$SortedParty == "WSR"]) - 3

incparty.ms.between <- incparty.between / dfnum

incparty.ms.within <- incparty.within / dfdenom

F.incparty <- incparty.ms.between / incparty.ms.within

obs.prob.incparty <- 1 - pf(F.incparty, dfnum, dfdenom)

# F on 2 and 323 df = 6.00 p = .003

### Just Dems

meand1 <- mean(vals3$V1[vals3$SortedParty == "SSD" | vals3$SortedParty == "UD" |
                          vals3$SortedParty == "WSD"])

meand2 <- mean(vals3$V2[vals3$SortedParty == "SSD" | vals3$SortedParty == "UD" |
                          vals3$SortedParty == "WSD"])

mean.res.length <- (meand1^2 + meand2^2)^.5

res.length <- length(vals3$V1[vals3$SortedParty == "SSD" | vals3$SortedParty == "UD" |
                                vals3$SortedParty == "WSD"]) * mean.res.length

ideol.mean.lengths <- c(.811, .630, .760)

incparty.between <- sum(ideol.mean.lengths * 
                          as.vector(table(vals3$SortedParty[vals3$SortedParty == "SSD" | 
                                                              vals3$SortedParty == "UD" |
                                                              vals3$SortedParty == "WSD"]))) - 
  res.length

incparty.within <- length(vals3$V1[vals3$SortedParty == "SSD" | 
                                     vals3$SortedParty == "UD" |
                                     vals3$SortedParty == "WSD"]) - 
  sum(ideol.mean.lengths * 
        as.vector(table(vals3$SortedParty[vals3$SortedParty == "SSD" | 
                                            vals3$SortedParty == "UD" | 
                                            vals3$SortedParty == "WSD"])))

dfnum <- 2

dfdenom <- length(vals3$V1[vals3$SortedParty == "SSD" | vals3$SortedParty == "UD" |
                             vals3$SortedParty == "WSD"]) - 3

incparty.ms.between <- incparty.between / dfnum

incparty.ms.within <- incparty.within / dfdenom

F.incparty <- incparty.ms.between / incparty.ms.within

obs.prob.incparty <- 1 - pf(F.incparty, dfnum, dfdenom)

# F on 2 and 339 df = 1.86, p = .157